#include "ProviderState.h"

#include "core/AppSignals.h"

ProviderState::ProviderState()
    : PropertyProvider()
{
}

void ProviderState::initEditor(
    QtTreePropertyBrowser* editor,
    shared_ptr<BaseElement> entity
)
{
    entity_ = dynamic_pointer_cast<uml::State>(entity);
    propertyToId.clear();
    PropertyProvider::initEditor(editor, entity);

    addProperty("isComposite", "isComposite", entity_->isComposite());
    addProperty("isLeaf", "isLeaf", entity_->isLeaf());
    addProperty("isOrthogonal", "isOrthogonal", entity_->isOrthogonal());
    addProperty("isSimple", "isSimple", entity_->isSimple());
    addProperty("isSubmachineState", "isSubmachineState", entity_->isSubmachineState());
    addProperty("name", "name", entity_->name());
    addProperty("qualifiedName", "qualifiedName", entity_->qualifiedName());
    QStringList visibilityNames;
    visibilityNames.append("public");
    visibilityNames.append("private");
    visibilityNames.append("protected");
    visibilityNames.append("package");
    addProperty("visibility", "visibility", static_cast<int>(entity_->visibility()), visibilityNames);

}

void ProviderState::valueChanged(QtProperty* property, bool value)
{
    QString id = propertyToId[property];
    if (id == "isComposite") {
        entity_->setIsComposite(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isLeaf") {
        entity_->setIsLeaf(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isOrthogonal") {
        entity_->setIsOrthogonal(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isSimple") {
        entity_->setIsSimple(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isSubmachineState") {
        entity_->setIsSubmachineState(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderState::valueChanged(QtProperty* property, double value)
{
    QString id = propertyToId[property];
    
}

void ProviderState::valueChanged(QtProperty* property, int value)
{
    QString id = propertyToId[property];
    if (id == "visibility") {
        entity_->setVisibility(static_cast<VisibilityKind>(value));
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderState::valueChanged(QtProperty* property, QString value)
{
    QString id = propertyToId[property];
    if (id == "name") {
        entity_->setName(value);
        emit appSignals->onElementRenamed(this, entity_);
    } else if (id == "qualifiedName") {
        entity_->setQualifiedName(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}
